<?php

namespace App\Console\Commands;

use Illuminate\Support\Facades\Storage;
use QL\QueryList;
use DB;
use Illuminate\Console\Command;

class tools extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tools {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'tools article | tools imgs | tools batch | tools keywords | tools crontabs';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $action = $this->argument('action');
        if ($action == 'article')
        {
            $this->article();
        }
        if ($action == 'imgs')
        {
            $this->imgs();
        }
        if ($action == 'batch')
        {
        	$this->batch();
        }
        if ($action == 'keywords')
        {
        	$this->keywords();
        }
        if ($action == 'crontabs')
        {
            $this->crontabs();
        }
    }
    
    public function crontabs()
    {
        $list   = DB::table('crontabs')->get();
        $data   = '#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
LANG=en_US.UTF-8
sleep 3  #等3秒后执行下一条
';
        foreach ($list as $v)
        {
            $data   .= $v->contents.PHP_EOL;
            $data   .= 'sleep 3  #等3秒后执行下一条'.PHP_EOL;
        }
        $data   .= '# 完成
echo "----------------------------------------------------------------------------"
endDate=`date +"%Y-%m-%d %H:%M:%S"`
echo "★[$endDate] Successful"
echo "----------------------------------------------------------------------------"';
        Storage::disk('public')->put('/shell/crontabs.sh', $data, 'public');
    }

    public function article()
    {
        $count  = DB::table('articles')->where('content', 'like', '%support-author%')->count();
        $num    = ceil($count/50);
        $this->info($num.'=='.$count);
        for($i=0;$i<$num;$i++)
        {
            $urls = [];
            $art = DB::table('articles')->where('content', 'like', '%support-author%')->skip($i*50)->take(50)->get();
            foreach ($art as $v)
            {
                $this->line($v->id.'=='.$v->title);
                $content = QueryList::html($v->content);
                $content->find('.support-author')->remove();
                $content->find('a')->remove();
                $value  = str_replace('<h3>#相关阅读#</h3>', '', $content->find('')->html());
                DB::table('articles')->where('id', $v->id)->update([
                    'content' => $value
                ]);
            }
        }

    }
    
    public function imgs()
    {
        $count  = DB::table('articles')->count();
        $num    = ceil($count/100);
        for($i=0;$i<$num;$i++)
        {
            $art = DB::table('articles')->skip($i*100)->take(100)->get();
            foreach ($art as $v)
            {
            	$content	= str_replace('https://img.7hnn.com/','https://img.6nww.com/',$v->content);
            	DB::table('articles')->where('id', $v->id)->update([
            		'content' => $content
            		]);
                $this->line($v->id);
            }
        }

    }
    
    public function batch()
    {
    	$count  = DB::table('sites')->count();
        $num    = ceil($count/50);
        $this->line($num.'=='.$count);
        for($i=64;$i<$num;$i++)
        {
        	$urls = [];
            $art = DB::table('sites')->where('state', 1)->skip($i*50)->take(50)->get();
            foreach ($art as $v)
            {
				$urls[] = env('APP_URL').'/siteinfo-'.$v->id.'.html';
            }
			$api = env('BAIDUZHANZHANG');
			$ch = curl_init();
			$options =  array(
			    CURLOPT_URL => $api,
			    CURLOPT_POST => true,
			    CURLOPT_RETURNTRANSFER => true,
			    CURLOPT_POSTFIELDS => implode("\n", $urls),
			    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
			);
			curl_setopt_array($ch, $options);
			$result = curl_exec($ch);
			$this->line($i);
			$this->line($result);
        }
    }
    
    public function keywords()
    {
        DB::table('tags')->truncate();
        $count  = DB::table('sites')->count();
        $num    = ceil($count/50);
        $this->line($num.'=='.$count);
        for($i=0;$i<$num;$i++)
        {
            $this->line($i.'==action');
            $urls = [];
            $art = DB::table('sites')->where('state', 1)->skip($i*50)->take(50)->get();
            foreach ($art as $v)
            {
                $this->normalizeTag(explode(',', $v->keywords));
            }
        }
    }
    
    function normalizeTag(array $tags)
    {
        return collect($tags)->map(function ($tag) {
            $newTag   = DB::table('tags')->where('name', $tag)->first();
            if($newTag) {
                return $newTag->id;
            }else {
                DB::table('tags')->insert(['name' => $tag]);
                return true;
            }
        })->toArray();
    }
}
